﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;

namespace BesAsm.Framework.ArcGisUtilities
{
  /// <summary>
  /// Contains extension methods for the IWorkspace interfaces and other ArcGIS
  /// interfaces that are implemented by Workspace objects.
  /// </summary>
  public static class WorkspaceExtensions
  {
    /// <summary>
    /// Deletes a feature class from the IWorkspace if it exists.
    /// </summary>
    /// <param name="workspace">The workspace.</param>
    /// <param name="featureClassName">Name of the feature class to delete.</param>
    public static void DeleteFeatureClass(this IWorkspace workspace, string featureClassName)
    {
      IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
      IDatasetName dsName = enumDatasetName.Next();
      while (dsName != null)
      {
        if (dsName.Name == featureClassName)
        {
          IFeatureWorkspace fws = workspace as IFeatureWorkspace;
          IFeatureClass fc = fws.OpenFeatureClass(featureClassName);
          IDataset yo = (IDataset)fc;
          if (yo.CanDelete())
            yo.Delete();
          else
            throw new System.IO.IOException("Unable to delete FeatureClass '" + featureClassName + "'");
        }
        dsName = enumDatasetName.Next();
      }
    }
  }
}
